init file /non-existent/file/to/test/failure does not exist
call stack .source .try_catch_recursive(body)
.global A \
.set SHELL /bin/rwsh
.nop .function .run_logic -- cmd [args ...] {.nop
  .signal_handler &{.internal_functions}$ {&&* {.argfunction}}
  .mapped_argfunction {&&* {.argfunction}}
  &&* {.argfunction}}
.function .before_command -- args ... [.{argfunction}] {.nop $args}
.function .after_command -- args ... [.{argfunction}] {.nop $args}
.function .prompt {.echo \$}
.function .vars {.internal_vars}
.source /etc/rwshrc-basic $argv$
#! /bin/rwsh --init-file
.function () {.nop}
.function # -- [ignored ...] [.{argfunction}] {.nop $ignored$}
.function fn -- prototype ... .{argfunction} {
  .function $prototype$ {.argfunction}}

# set up environment
.set_max_collectible_exceptions 10
.set_max_extra_exceptions 5
.global nl (
)
.if .var_exists ? {}
.else {.global ? (); .nop $?}
.nop $FIGNORE

# internal functions
.function .ambiguous_prototype_dash_dash prototype stack ... {
  .echo $prototype is an ambiguous location for a prototype --
  .combine ( (it requests reading the user's mind) ) \( stack: $stack \) $nl}
.function .arguments_for_argfunction name {
  echo cannot specify arguments for $name directly}
.function .bad_argfunction_style style {
  echo $style is neither an argfunction nor a substitution nor a brace literal}
.function .bad_args -- proto assigned needed excess call_stack ... {
  .combine (unacceptable arguments: for prototype ) $proto , $nl
  if_only .test_not_empty $assigned {
    .combine ( assigned )
    .for $assigned$ (-- var val) {.combine $var = ${paren_if_words $val} ( )}
    .combine , $nl ( but)}
  if_only .test_not_empty $needed {
    .combine ( needed \() $needed \), $nl
    if_only .test_not_empty $excess {.combine ( and)}}
  if_only .test_not_empty $excess {
    .combine ( found \() $excess (\) excess) $nl}
  echo \ call stack:  $call_stack}
.function .bad_if_nest stack ... {
  echo nested .if without .else with call stack: $stack$}
.function .binary_does_not_exist name [stack ...] {
  echo $name : binary does not exist
  if_only .var_exists stack {echo call stack: $stack}}
.function .binary_not_found name path stack ... {
  echo $name : binary not found in $path
  echo call stack: $stack$}
.function .break stack ... {
  echo .break thrown outside of control flow body
  echo call stack: $stack$}
.function .continue stack ... {
  echo .continue thrown outside of control flow body
  echo call stack: $stack$}
.function .dash_dash_argument args stack ... {
  echo optional -- parameter cannot take arguments \( $args \) with call stack: $stack$}
.function .dash_star_argument args stack ... {
  echo optional -* parameter cannot take arguments \( $args \) with call stack: $stack$}
.function .directory_not_found -- dir stack ... {
  .combine $dir ( directory does not exist) $nl
  echo call stack $stack}
.function .divide_by_zero numerator stack ... {
  echo attempt to divide $numerator by 0 with call stack: $stack$}
.function .double_redirection first second [stack ...] {
  echo double redirection is not supported at this time: $first and $second
  if_only .var_exists stack {echo call stack: $stack$}}
.function .duplicate_parameter -- parameter stack ... {
  echo duplicated parameter in function definition: $parameter
  echo call stack: $stack$}
.function .elipsis_first_arg -- elipsis stack ... {
  echo $elipsis elipsis cannot be the first argument in a prototype
  echo call stack: $stack$}
.function .elipsis_out_of_option_group -- group stack ... {
  echo $group for elipsis to apply to option it must occur within the brackets
  echo call stack: $stack$}
.function .else_without_if stack ... {
  echo else without if with call stack: $stack$}
.function .epsilon var_val change stack ... {
  echo $change is too small relative to $var_val to modify its value
  echo call stack: $stack$}
.function .excess_argfunction cmd [stack ...] {
  echo $cmd does not accept an argfunction with call stack: $cmd $stack$}
.function .excessive_exceptions_collected max stack ... {
  echo exceeded maximum number of exceptions \( $max \) in collect_errors.
  echo call stack: $stack$}
.function .excessive_exceptions_in_catch max stack ... {
  echo exceeded maximum number of exceptions \( $max \) in try_catch.
  echo call stack: $stack$}
.function .excessive_nesting stack ... {
  echo function exceeded max nesting \( ${.get_max_nesting} \). call stack: $stack}
.function .exec_failed binary errno stack ... {
  .combine (.exec failed for ) $binary ( with errno ) $errno $nl
  echo call stack $stack}
.function .executable_already_exists -- binary stack ... {
  echo $binary is already an executable in the hash table $nl
  echo call stack $stack}
.function .failed_substitution substitution [stack ...] {
  echo substitution $substitution failed
  if_only .var_exists stack {echo call stack: $stack$}}
.function .false test [stack ...] {
  echo assertion $test failed
  echo call stack $stack$}
.function .file_not_found -- file stack ... {
  echo could not find file $file
  echo call stack $stack}
.function .file_open_failure -- file stack ... {
  echo could not open file $file
  echo call stack $stack}
.function .fixed_argument -- arg stack ... {
  echo $arg : fixed arguments are not supported at this time
  echo call stack $stack}
.function .flag_in_elipsis -- flag stack ... {
  echo found flag $flag while collecting arguments for elipsis
  echo call stack $stack$}
.function .function_not_found -- cmd [stack ...] {
  echo $cmd : command not found \( $cmd $stack$ \)}
.function .global_would_be_masked name stack ... {
  .echo requested global $name already exists as a local
  echo . It would be masked by the local and inaccessible from here
  echo call stack $stack$}
.function .help [args ...] {
  .nop $args$
  .echo this is the default configuration for rwsh \(or something copied from it
  echo \), documentation should be available at rwsh.sf.net or rwsh.org}
.function .if_before_else stack ... {
  echo two .if commands without a .else between them with call stack : $stack$}
.function .illegal_function_name name stack ... {
  .combine \$ $name ( cannot be a function) $nl
  echo call stack $stack}
.function .illegal_variable_name name stack ... {
  .combine \$ $name ( cannot be set) $nl
  echo call stack $stack}
.function .input_range -- value stack ... {
  echo value $value is out of usable numeric range
  echo call stack $stack$}
.function .internal_error -- [args ...] {
  echo internal error: $args$}
.function .interrupted_sleep [stack ...] {
  echo sleep was interrupted
  echo call stack $stack$}
.function .invalid_word_selection selection [stack ...] {
  echo $selection is not a valid word selection
  echo call stack $stack$}
.function .mismatched_brace context [stack ...] {
  echo mismatched brace: $context $stack$}
.function .mismatched_bracket context [stack ...] {
  echo mismatched bracket: $context
  if_only .var_exists stack {echo call stack: $stack$}}
.function .mismatched_parenthesis string [stack ...] {
  .echo signal triggered: $0 \( $string $stack$ \); .combine $nl
  echo mismatched parenthesis: $string
  echo call stack: $stack$}
.function .unclosed_parenthesis string [stack ...] {
  .echo signal triggered: $0 \( $string $stack$ \); .combine $nl
  echo mismatched parenthesis: $string
  echo call stack: $stack$}
.function .missing_argfunction cmd [add_stack ...] {
  echo $cmd requires an argfunction
  if_only .var_exists add_stack {echo stack $add_stack$}}
.function .multiple_argfunctions {
  echo multiple argfunctions not supported at this time}
.function .not_a_directory file stack ... {
  .combine $file ( is not a directory) $nl
  echo call stack $stack}
.function .not_a_function executable stack ... {
  .combine $executable ( is not a function) $nl
  echo call stack $stack}
.function .not_a_number -- nan stack ... {
  echo $nan is not a number
  echo call stack: $stack$}
.function .not_catching_exception stack ... {
  echo .replace_exception called outside of an exception handler
  echo call stack: $stack$}
.function .not_executable file stack ... {
  echo $file is not executable
  echo call stack: $stack$}
.function .not_soon_enough too_late [stack ...] {
  echo $too_late attempts to substitute after the command has been run
  if_only .var_exists stack {echo call stack $stack}}
.function .post_elipsis_option -- flag stack ... {
  echo option $flag coming after an elipsis has no meaning
  echo call stack: $stack$}
.function .post_dash_dash_flag -- flag stack ... {
  echo flag option $flag occurs after -- in the prototype
  echo call stack: $stack$}
.function .prompt {.echo \$}
.function .result_range -- lhs rhs op [stack ...] {
  echo result of $op with arguments $lhs and $rhs is out of range
  echo call stack: $op $stack$}
.function .return_code code cmd [add_stack ...] {
  .if .test_in $cmd /usr/bin/test /bin/false {
    .replace_exception .false $cmd
    .nop $code $add_stack$}
  .else {
    .set ? $cmd returned $code
    echo $?$ : call stack $cmd $add_stack$}}
.function .selection_not_found not_found selection [stack ...] {
  .echo no file matching pattern $not_found
  if_only .test_string_unequal $not_found $selection {
    .echo () for selection $selection}
  .echo $nl
  if_only .var_exists stack {echo call stack: $stack$}}
.function .sigcont {
  echo received SIGCONT}
.function .sigchld {
  echo received SIGCHLD}
.function .sighup {
  echo received SIGHUP}
.function .sigint {
  .throw echo $nl received SIGINT}
.function .sigquit {
  echo received SIGQUIT}
.function .sigpipe {
  echo received SIGPIPE}
.function .sigterm {
  .throw echo $nl received SIGTERM}
.function .sigtstp {
  echo received SIGTSTP}
.function .siginfo {
  echo received SIGINFO}
.function .sigusr1 {
  echo received SIGUSR1}
.function .sigusr2 {
  echo received SIGUSR2}
.function .sigunknown {
  echo received unknown unix signal}
.function .shutdown -- [args ...] {.nop $args}
.function .tardy_flag -- flag stack ... {
  echo $flag flag comes too late in command
  echo call stack: $stack$}
.function .unchecked_variable -- var [stack ...] {
  echo $var was not checked with call stack $stack$}
.function .unclosed_brace context [stack ...] {
  echo unclosed brace: $context $stack$}
.function .undeclared_variable -- name [stack ...] {
  echo $name was not declared
  echo call stack: $stack$}
.function .undefined_variable -- name [stack ...] {
  echo optional variable $name was not defined
  echo call stack: $stack$}
.function .unfinished_if_block [stack ...] {
  echo conditional not finished within preceeding block
  if_only .var_exists stack {echo call stack: $stack$}}
.function .unreadable_dir dir errno [stack ...] {
  .combine (cannot read directory ) $dir \ \(errno\  $errno \) $nl
  echo call stack: $stack$}
.function .unrecognized_flag -- flag cmd [add_stack ...] {
  echo $flag flag is unrecognized for $cmd
  echo call stack: $cmd $add_stack$}
.function .unused_before_set -- var [stack ...] {
  echo attempt to set $var before making use of its initial value
  echo call stack $stack$}
.function .unused_variable -- var [stack ...] {
  echo $var was unused with call stack $stack$}
.function .variable_already_exists name stack ... {
  echo variable $name already exists
  echo call stack $stack$}
.function .version_incompatible version stack ... {
  echo this shell is not compatible with requested version $version
  echo call stack: $stack$}

# ability of functions to act as builtin wrappers
fn ! args ... {
  .if $args$ {.throw .false ${.echo ! $args}}
  .else {.nop}}
fn af -- [argv ...] .{argfunction} {.argfunction}
fn c -- text ... {.combine $text$}
fn e -- text ... {.echo $text}
fn do_while argv ... .{argfunction} {
  .try_catch_recursive .break {
    .nop $argv
    .try_catch_recursive .continue {.argfunction}
    .while $argv$ {.argfunction}}}
fn echo -- text ... {.echo $text; .combine $nl}
fn echo-comments argv ... {
  .stepwise $argv$ (-- cmd [args ...]) {.nop $args$
    if_only .test_in $cmd .nop # #! ## {echo &&&*}}}
fn exec -- argv ... {.exec $argv$}
fn elif command ... .{argfunction} {
  .else_if $command$ {.argfunction}}
fn else .{argfunction} {.else {.argfunction}}
fn fns -- name args ... .{argfunction} {fn $name [-?] $args$ {.argfunction}}
fn if_only -- args ... .{argfunction} {
  .if $args$ {.argfunction}; .else {.nop}}
fn if_only_not -- args ... .{argfunction} {
  .if $args$ {.nop}; .else {.argfunction}}
fn ntimes n .{argfunction} {
  .while test_var_greater n 0 {
    .mapped_argfunction {.argfunction}
    .var_subtract n 1}}
fn outer_break stack ... {.replace_exception .break $stack$}
fn outer_continue stack ... {.replace_exception .continue $stack$}
fn forj -- [vals ...] .{argfunction} {.for $vals$ (-- j) {.argfunction}}
fn foreok -- [items ...] prototype .{argfunction} {
  .if .var_exists items {.for $items$ $prototype {.argfunction}}
  .else {.nop $prototype}}
fn outer_for -- vals ... var .{argfunction} {
  .for $vals$ $var {
    .try_catch_recursive outer_break outer_continue {.argfunction}}}
fn outer_while -- argv ... .{argfunction} {
  .while $argv$ {
    .try_catch_recursive outer_break outer_continue {.argfunction}}}
fn setf -- var value {
  .if .var_exists $var {.set $var $value}
  .else {.global $var $value}}
fn single -- args ... {.scope ${$args$}$ result {echo $result}}
fn test_var_not_equal var value {.test_string_unequal $$var $value}
fn test_var_greater -- var value {.test_greater $$var $value}
fn type [-t] args ... [.{argfunction}] {
  .for $args$ (-- name ...) {
    .try_catch_recursive .function_not_found {
      if_only_not .var_exists -t {.echo &&&name is ()}
      .type &&&1 {.argfunction}}}}
fn whence command [.{argfunction}] {
  .try_catch_recursive .function_not_found {
    .whence_function $command {.argfunction}}}
fn whichp binary {.which_path $binary $PATH}
fn while_and_one_more argv ... .{argfunction} {
  .try_catch_recursive .break {
    .while $argv$ {.argfunction}
    .try_catch_recursive .continue {.argfunction}}}
fn var_exists -- var {.var_exists $var}
fn var_in var set ... {.test_in $$var $set$}
fn var_less var N {.test_less $$var $N}
fn var_val -- [vars ...] {
  .if .var_exists vars {.for $vars$ (-- v ...) {.combine $v \( $$v \) \ }}
  .else {.nop}}
fn paren_if_words -- value {
  .if .test_number_equal ${.argc $value$} 1 {.combine $value}
  .else {.combine \( $value \)}}
fn polish -- operator builtin {
  fn &&operator -- lhs rhs ... {
    if_only .test_in $lhs + - / * {
      .scope ${$lhs $rhs$}$ (-- l r ...) {
        .set lhs $l
        .set rhs $r}}
    .scope $rhs$ (rhs [extra ...]) {
      .if .test_in $rhs + - / * {
        .scope ${$rhs $extra$}$ (rhs [extra ...]) {
          &&builtin lhs $rhs
          echo $lhs $extra$}}
      .else {
        &&builtin lhs $rhs
        echo $lhs $extra$}}}}
polish + .var_add
polish / .var_divide
polish * .var_multiply
polish - .var_subtract

# external commands tested in the test suite
fn false {.try_catch_recursive .return_code {${whichp false}}}
fn ln [-?] [-t directory] target ... {
  .nop $-* $target # in case the following assertion fails
  .fork .mapped_argfunction {
    if_only .var_exists -t {.cd $directory}
    .for $target$ (-- j ...) {.test_file_exists $j}
    &{.which_path ln /bin:/usr/bin} $-? -t . $target$}}
fn test -- args ... {.try_catch_recursive .return_code {${whichp test} $args$}}
if_only .var_exists argv {.error ignored excess arguments $argv $nl}
.function .shutdown -- args ... {
  .nop $args
  .combine $nl (now terminating normally) $nl}
$.function a [-*] [-x b ...] c {
  forj &&{.list_locals}$ j {.combine $j \( $$j \) \ }; .echo $nl}
$.nop $A $FIGNORE
$.whence_function a
.function a [-?] [-x b ...] c {
  forj &{.list_locals}$ j {.combine $j \( $$j \) ( )}; .echo $nl}
$a first
-*() -?() c(first) j(j) 
$a -x first
unacceptable arguments: for prototype [-?] [-x b ...] c,
 assigned -*=(-x first) -?=() -x=(-x first) b=first ,
 but needed (c),
( call) stack: a
$a -x (fi rst) second
-*(-x (fi rst)) -?() -x(-x (fi rst)) b((fi rst)) c(second) j(j) 
$a -x first (sec ond) third
-*(-x first (sec ond)) -?() -x(-x first (sec ond)) b(first (sec ond)) c(third) j(j) 
$a -x (fi rst) (sec ond) third fourth
-*(-x (fi rst) (sec ond) third) -?() -x(-x (fi rst) (sec ond) third) b((fi rst) (sec ond) third) c(fourth) j(j) 
$.function a [-*] [-x ... b] c {
  forj &&{.list_locals}$ {.combine $j \( $$j \) \ }
  .combine $nl
  # if_only .test_not_empty $-* {c (-*: ) $-*$ $nl}
  # if_only .var_exists -x {c (-x: ) $-x$ $nl}
  # if_only .var_exists b {c (b: ) $b$ $nl}
  # if_only .var_exists c {c (c: ) $c$ $nl}
}
$.whence_function a
.function a [-?] [-x ... b] c {
  forj &{.list_locals}$ {.combine $j \( $$j \) ( )}
  .combine $nl
  # if_only .test_not_empty $-* {c (-*: ) $-*$ $nl}
  # if_only .var_exists -x {c (-x: ) $-x$ $nl}
  # if_only .var_exists b {c (b: ) $b$ $nl}
  # if_only .var_exists c {c (c: ) $c$ $nl}
}
$a first
-*() -?() c(first) 
$a -x first
unacceptable arguments: for prototype [-?] [-x ... b] c,
 assigned -*=(-x first) -?=() -x=(-x first) b=first ,
 but needed (c),
( call) stack: a
$a -x (fi rst) second
-*(-x (fi rst)) -?() -x(-x (fi rst)) b((fi rst)) c(second) 
$a -x first (sec ond) third
-*(-x first (sec ond)) -?() -x(-x first (sec ond)) b((sec ond)) c(third) 
$a -x (fi rst) (sec ond) third fourth
-*(-x (fi rst) (sec ond) third) -?() -x(-x (fi rst) (sec ond) third) b(third) c(fourth) 
$
now terminating normally
